package exceptiondemo;

import java.util.*;

/**
 * @Author 12629
 * @Date 2022/3/3 18:36
 * @Description：
 */
class Person implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class MyException extends RuntimeException{
    public MyException() {
        super();
    }

    public MyException(String s) {
        super(s);
    }
}
//编译时异常/受查异常
class MyException2 extends Exception{
    public MyException2() {
        super();
    }

    public MyException2(String s) {
        super(s);
    }
}
public class TestDemo {
    public static void function2() throws MyException2{
        int a = 10;
        if(a == 10) {
            throw new MyException2("哈哈  我自己写的异常!");
        }
    }

    public static void function3() {
        try {
            int a = 10 ;
            if(a == 10) {
                throw new MyException2("哈哈  我自己写的异常!");
            }
        }catch (MyException2 e) {

        }
    }


    public static void main(String[] args) {
        /*try {
            function();
        }catch (exceptiondemo.MyException e) {
            e.printStackTrace();
            System.out.println("真的捕捉到了吗？？？");
        }
        System.out.println("正常的代码！");*/
    }

    public static void function() throws MyException{
        int a = 10;
        if(a == 10) {
            throw new MyException("哈哈  我自己写的异常!");
        }
    }

    public static void main14(String[] args) {
        try {
            function();
        }catch (MyException e) {
            e.printStackTrace();
            System.out.println("真的捕捉到了吗？？？");
        }
        System.out.println("正常的代码！");
    }


    public static void func4() {
        int[] array = {1,2,3,4};
        System.out.println(array[9]);
    }

    public static void main12(String[] args) {
        try {
            func4();
        }catch (ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
            System.out.println("组越界了！");
        }
        System.out.println("这是正常的其他的业务逻辑");
    }


    public static int fun3() {
        try {
            return 10;
        }finally {
            return 1;
        }
    }



    public static void main11(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            int a = scanner.nextInt();
            System.out.println(a);
        } catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入的参数不匹配！");
        }
        System.out.println("FDSAfsaafa");
    }




    public static int func2() {
        try {
            int a = 10;
            return a+10;
        }catch (NullPointerException e) {
            System.out.println("haha");
        }finally {
            return 999;//不建议在这里写return
        }
    }

    public static void main10(String[] args) {
        System.out.println(func2());
    }


    public static void main9(String[] args) {
        Scanner scanner = new Scanner(System.in);
        /*int a = scanner.nextInt();
        System.out.println(a);*/
        try {
            int a = scanner.nextInt();
            System.out.println(a);
        }catch (InputMismatchException e) {
            e.printStackTrace();
            System.out.println("输入的参数不匹配！");
        }finally {
            scanner.close();
        }
        System.out.println("FDSAfsaafa");
    }

    public static void main8(String[] args) {
        int[] array = {1, 2, 3, 4};
        try {
            array = null;
            System.out.println(array[0]);
        } catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("捕获了空指针异常！");
        }finally {
            System.out.println("来关闭一些资源");
        }
        System.out.println("hahahhh ");
    }

    public static void main7(String[] args) {
        int[] arr = {1, 2, 3};
        try {
            System.out.println("before");
            //arr = null;
            System.out.println(arr[100]);
            System.out.println("after");
        } catch (NullPointerException e) { // Exception可以捕获到所有异常
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("fdsfaafa");
            e.printStackTrace();
        }

        System.out.println("after try catch");
    }

    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4};
        try {
            System.out.println(array[0]);
            System.out.println(10 / 0);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("捕获了    异常！");
        }
    }


    public static void main5(String[] args) {
        int[] array = {1, 2, 3, 4};
        try {
            System.out.println(array[0]);
            System.out.println(10 / 0);
        } catch (ArrayIndexOutOfBoundsException | ArithmeticException | NullPointerException e) {
            System.out.println("捕获了    异常！");
        }
    }


    public static void main4(String[] args) {
        int[] array = {1,2,3,4};
        try {
            System.out.println(array[0]);
            System.out.println(10/0);
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("捕获了数组越界的异常！");
        }catch (ArithmeticException e) {
            System.out.println("算术异常！");
        }
    }

    public static void func() throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person)person.clone();
    }
    public static void main3(String[] args) {
        try {
            func();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public static void main1(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        Person person1 = (Person)person.clone();
    }

    public static void main2(String[] args){
        Person person = new Person();
        try {
            Person person1 = (Person)person.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
